package com.top.mall.base

import android.app.Application
import androidx.lifecycle.MutableLiveData
import com.blankj.utilcode.util.GsonUtils
import com.module.lemlin.http.HttpResponseBody
import com.module.lemlin.http.HttpResponseData
import com.module.lemlin.http.SubscribeViewModel
import com.module.lemlin.owner.OwnerViewModel
import com.top.mall.app.BaseApplication
import com.top.mall.app.SPSConstance
import com.top.mall.entity.*
import com.top.mall.main.access.AccessActivity
import rxhttp.RxHttp
import rxhttp.await

open class BaseViewModel(application: Application) : OwnerViewModel(application) {


    override fun onTokenOverdue() {
        AccessActivity.open()
    }


    val userIndexLiveData = MutableLiveData<HttpResponseBody<UserIndexResponse>>()

    /**
     * 首页数据
     * */
    fun userIndex() {
        setRxLifeScope(object : SubscribeViewModel<UserIndexResponse>(userIndexLiveData) {
            override fun onSuccess(resp: UserIndexResponse) {
                super.onSuccess(resp.apply {
                    SPSConstance.userInfo = data.userInfo.apply {
                        goods_collect_count = data.goods_collect_count
                        cash_coupon_sum = data.cash_coupon_sum
                    }
                })
            }
        }) {
            RxHttp.get("/api/user/index")
                .add("warehouse_no", BaseApplication.WAREHOUSE_NO)
                .await<UserIndexResponse>()
        }
    }
    //

    val userAreaLiveData = MutableLiveData<HttpResponseBody<AreaListResponse>>()
    fun userArea(province: Int = 0, city: Int = 0, grade: Int) {
        setRxLifeScope(object : SubscribeViewModel<AreaListResponse>(userAreaLiveData) {
            override fun onSuccess(resp: AreaListResponse) {
                super.onSuccess(resp.apply { this.grade = grade })
            }
        }) {
            val http = RxHttp.get("/api/common/area")
            if (province > 0) {
                http.add("province", province)
            }
            if (city > 0) {
                http.add("city", city)
            }
            http.await<AreaListResponse>()
        }
    }

    val userProfileLiveData = MutableLiveData<HttpResponseBody<HttpResponseData>>()
    fun userProfile(profile: Profile) {
        setRxLifeScope(SubscribeViewModel(userProfileLiveData)) {
            val maps: Map<String, String?> =
                GsonUtils.fromJson<Map<String, String?>>(
                    GsonUtils.toJson(profile),
                    Map::class.java
                ).filter { it.value != null }
            RxHttp.postForm("/api/user/profile")
                .addAll(maps)
                .await<HttpResponseData>()
        }
    }

    val userCollectLiveData = MutableLiveData<HttpResponseBody<GoodsCollectResponse>>()
    fun userCollect() {
        setRxLifeScope(SubscribeViewModel(userCollectLiveData)) {
            RxHttp.postForm("/api/user/collectList")
                .add("warehouse_no", BaseApplication.WAREHOUSE_NO)
                .await<GoodsCollectResponse>()
        }
    }


    //

    val userShopLiveData = MutableLiveData<HttpResponseBody<ShopListResponse>>()
    fun userShop() {
        setRxLifeScope(SubscribeViewModel(userShopLiveData)) {
            RxHttp.postForm("/api/shops/shopList")
                .add("lng", "119.306126")
                .add("lat", "26.059226")
                .await<ShopListResponse>()
        }
    }

    val userInvoiceLiveData = MutableLiveData<HttpResponseBody<UserInvoiceResponse>>()
    fun userInvoice() {
        setRxLifeScope(SubscribeViewModel(userInvoiceLiveData)) {
            RxHttp.get("/api/invoice/invoiceList")
                .add("status", 2)
                .await<UserInvoiceResponse>()
        }
    }

    //
    val userDeliveryListLiveData = MutableLiveData<HttpResponseBody<UserDeliveryListResponse>>()
    fun userDeliveryTime() {
        setRxLifeScope(SubscribeViewModel(userDeliveryListLiveData)) {
            val response = mutableListOf<UserDeliveryList>()
            val deliveryTime0 = RxHttp.get("/api/cart/checkDeliveryTime")
                .add("shop_id", BaseApplication.SHOP_ID)
                .add("day", 0)
                .await<UserDeliveryTimeResponse>()
            if (deliveryTime0.code == 1) {
                response.add(UserDeliveryList("今天", 0, deliveryTime0.data))
            }
            val deliveryTime1 = RxHttp.get("/api/cart/checkDeliveryTime")
                .add("shop_id", BaseApplication.SHOP_ID)
                .add("day", 1)
                .await<UserDeliveryTimeResponse>()
            if (deliveryTime1.code == 1) {
                response.add(UserDeliveryList("明天", 1, deliveryTime1.data))
            }
            return@setRxLifeScope UserDeliveryListResponse(data = response).apply {
                code = deliveryTime1.code
                msg = deliveryTime1.msg
            }
        }
    }


    val userAboutLiveData = MutableLiveData<HttpResponseBody<UserAboutResponse>>()
    fun userAbout(type:String) {
        setRxLifeScope(SubscribeViewModel(userAboutLiveData)) {
            RxHttp.get("/api/article/detail")
                .add("type", type)
                .await<UserAboutResponse>()
        }
    }
}